LB Server - CGI with LB

© 2004, Colin McMurchie

author contact:

c.mcmurchie@livjm.ac.uk

Home

Tip Corner

API Corner

Qcard DLL #5

Sprite Byte

Projectile Motion

Sub Handlers

Agent Lesson

LB Server

A Web Presence

Website Review

An Interview

DoubleClick

Disk Cleaner

Newsletter help

Index


Using Liberty Basic with a web server

Do you run a web server? Is your web server administrator friendly? If so, you might want to use LB to process scripts. If you are familiar with some html, and understand something of how servers operate, then read on.

There has been talk on the LB forums about making servers with LB for use on the Internet. This got me thinking about the possibility of using LB programmes to write dynamic web pages, through an existing server, in the same way that PERL scripts do. This article is an account of how I approached the problem, and the sort of solution I found, which I am sure others could improve upon. This requires that 'Server Side Includes' are enabled on the server in use.

Why would anybody want to use LB for dynamic web pages anyway? Surely there are better ways of doing things? Well of course there are. No-one would rationally start to learn to use LB for this purpose, when there are other means available. But, if you already have a grasp of LB, you might want to use your knowledge in a new context, rather that learn a whole new language for what might be a small project. Besides, I am a curious person. I like solving problems, even if they have little practical use.

How the process works.

The key to running an LB programme to create dynamic web pages lies within a single support file, of two significant lines - "callbasic.shtml". The first is:-

This line does the business of running the LB programme. The programme then produces a file called 'tempfile.html'

the next line of 'callbasic.shtml' is:-

this line then takes a file produced by LB, and includes it within "callbasic.shtml", as if it was always a part of that page. All the scripts in the demonstration work through this file.

How to set up the system

I have tested this with a 'local server' using a copy of Apache for Windows ( a widely available free http server). I first created a directory to hold my LB files, which I called 'lb_files', and I placed this within the 'htdocs' directory. the full path to this directory, on my machine, was

"c:/program files/apache group/apache/htdocs/lb_files"

Copy into this directory the LB dll and sll support files and a copy of the runtime engine. Rename it to 'bananas.exe' (for want of a better name). Make bananas.tkn from the included 'bananas.bas' file. Make sure that 'Callbasic.shtml' is placed here as well. I also placed the html files that called 'callbasic.shtml', for convenience.

Next prepare the server to support LB. My version of Apache is 1.3.29 for Windows, but the process I think would be the same for any other version. It was necessary to make two changes to the default 'httpd.conf' file. first the following lines were added, in the part of the file concerned with Directories.

<Directory "C:/Program Files/Apache Group/Apache/htdocs/lb_files">
        Options +Includes
        </Directory>

You will need to edit the first of these lines to match your own directory structure.

Then the following two lines were un-commented

AddType text/html .shtml
AddHandler server-parsed .shtml

Once Apache is restarted, you should be able to run liberty basic scripts.

Making the files

In order to avoid having too many LB runtime engines building up on my hard disk, I decided that I would run all my LB pages from one LB .tkn file. This meant that I needed to have some means of identifying which 'page' I wanted to run each time I called LB. This I decided to do my calling LB through a form, the first, hidden, field of which contained a ley to select the right response from LB. To use this approach do the following.

Create an HTML page with a form in it. Use one of the included forms as a template. Make sure that the "action" for the form is 'callbasic.shtml', and the "method" is GET. The first field in the form should be of type "hidden" and the value of this field should correspond to a selector within your liberty basic script. You may use up to 99 other form elements after this.

At the liberty basic end, open "bananas.bas" in the project directory. Go to the "select Case" section towards the start of the programme, and simply insert two lines such as

Case "myscript1"
call mysub1" 

then add a subroutine called 'mysub1' at the end of the code. So long as this writes valid html to a file called 'tempfile.html' then everything should be fine. Run the programme in the editor to ensure there are no problems. If everything is OK you should be returned to the Editor almost immediately. Finally compile a tkn called Bananas.tkn to the same directory. You are then in business. You can try all this out with the demonstration files included here. Navigate to your chosen directory and run the 'Index.html' file. This gives three options. The first simply demonstrates the fact that from values are captured by LB. the second show a simple calculation performed by LB, and the third renders some .jpg files (using examples borrowed from a recent edition of this publication!).

What you can do and what you cannot do this way

Your LB generated page must include only valid HTML statements, made with

 print #text, " Your text here " 

statements. This may seem limited, but all web pages are, ultimately, built in the same manner. Within the HTML, you can include references to images to display, and perhaps to animations and sound files etc.

What you cannot do is use any GUI functions in your scripts. The user has no way of interacting with them. Even simple notices are dangerous. You may want to use them at key points in your script for testing, but you should not leave them in when you 'go live'.

What you can use LB scripts for

You can use them for databases. You can use them for text/jpeg based adventure games. You can use them for catalogues and picture galleries. You cannot use them for Shoot'em ups (shame).

Technical notes

First a disclaimer - I have not tested this for real, as I do not have suitable access to a real world server. I have used it with an Apache server on my own machine using loop back to Localhost. If anyone can set it up on a real server I would be interested in finding out how well it works.

All forms are eventually processed by a single LB runtime engine and a single tkn. The SELECT CASE is used to find the actual script to use, based on the first field in the form. This avoids the need for many separate TKN files, and more importantly, many separate runtime engines. I did consider the possibility of renaming the run-time engine 'on the fly', but could not get it to work. Of course, if you have several separate projects and you want clean code, you could use different runtime engines and different tkns.

[Editors Note: this article comes with several small project files, which include a basic source code module, html pages and other files. To use the project you must download the newsletter, have a web server on hand and configure it as described above. Find the project in the archive named LBserver.zip in the newsletter archive.]


Home

Tip Corner

API Corner

Qcard DLL #5

Sprite Byte

Projectile Motion

Sub Handlers

Agent Lesson

LB Server

A Web Presence

Website Review

An Interview

DoubleClick

Disk Cleaner

Newsletter help

Index